package view;
 
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

import model.Lap;
import model.Temps;
import controller.Controladora;
 
@SuppressWarnings("serial")
public class PlotTable extends JPanel {
	
    private boolean DEBUG = false;
    private PlotStatistics plot;
    private int voltes; 
    private Object[][] data;
    private JTable table;
    private Controladora controladora;
 
    public PlotTable(final Controladora controladora) {
        super(new GridLayout(2,1));

        this.controladora = controladora;
		plot = new PlotStatistics(controladora);
		
		ArrayList<Lap> laps = controladora.getCircuit().getLaps();
		data = new Object[laps.size()][8];
		
        table = new JTable(new PlotTableModel(data)); 
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);
        
       //Listeners table
        table.getModel().addTableModelListener(new TableModelListener() {

        
			@Override
			public void tableChanged(TableModelEvent e) {
				int row = e.getFirstRow();
		        int column = e.getColumn();
		        TableModel model = (TableModel)e.getSource();
		        String columnName = model.getColumnName(column);
		        Object data = model.getValueAt(row, column);
		        plot.Actualitza();
		       
		        if(columnName.equals("ShowLap") && row < controladora.getCircuit().getLaps().size()) {
		        	controladora.voltaActiva(row, (Boolean)data);
		        }
			}
		});
        
        //Color Renderer 
        //table.setDefaultRenderer(Color.class, new ColorRenderer(true));
        table.setDefaultRenderer(Object.class,new YourTableCellRenderer(controladora.getCircuit().getLaps().size()));
        
       
        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setVisible(true);
        //Add the scroll pane to this' panel.
        add(scrollPane);
        
        //Add Graphic statistics.
        add(plot);
        
  
    }

    /**
     * Actualitza la taula
     */
	public void updateTable() {
		((YourTableCellRenderer)table.getDefaultRenderer(Object.class)).setFilas(controladora.getCircuit().getLaps().size());
		
		int RGB[] = new int[3];
		int row = 0;
		try {
		float bestTime = controladora.getCircuit().getStatistics().getBestTime().getValor();
		float bestSpeed = controladora.getCircuit().getStatistics().getHighestMaximumSpeed();
		float bestAvgSpeed = controladora.getCircuit().getStatistics().getHighestAverageSpeed();
		float bestMinSpeed = controladora.getCircuit().getStatistics().getHighestMinimumSpeed();
		
		((PlotTableModel)table.getModel()).setData(new Object[(controladora.getCircuit().getLaps().size()) + 2][8]);
		for (int i = 0; i < controladora.getCircuit().getLaps().size();i++){
			
			if (bestTime == controladora.getCircuit().getLaps().get(i).getTime().getValor()) {
				((YourTableCellRenderer)table.getDefaultRenderer(Object.class)).setBestTimeIndex(i);
			}
			
			if (bestSpeed == controladora.getCircuit().getLaps().get(i).getMax_vel()) {
				((YourTableCellRenderer)table.getDefaultRenderer(Object.class)).setTopSpeedIndex(i);
			}
			
			if (bestAvgSpeed == controladora.getCircuit().getLaps().get(i).getAvg_vel()) {
				((YourTableCellRenderer)table.getDefaultRenderer(Object.class)).setAvgSpeedIndex(i);
			}
			
			if (bestMinSpeed == controladora.getCircuit().getLaps().get(i).getMin_vel()) {
				((YourTableCellRenderer)table.getDefaultRenderer(Object.class)).setMinSpeedIndex(i);
			}
			
			table.getModel().setValueAt(controladora.getCircuit().getLaps().get(i).isMostrar(), i, 0);
			table.getModel().setValueAt("Lap " + (i+1), i, 1);
			String s = controladora.getCircuit().getLaps().get(i).getTime().toString();
			table.getModel().setValueAt(s, i, 2);
			table.getModel().setValueAt(controladora.getCircuit().getLaps().get(i).getMax_vel(), i, 3);
			table.getModel().setValueAt(controladora.getCircuit().getLaps().get(i).getAvg_vel(), i, 4);
			table.getModel().setValueAt(controladora.getCircuit().getLaps().get(i).getMin_vel(), i, 5);
			table.getModel().setValueAt(controladora.getCircuit().getLaps().get(i).getLenght(), i, 6);
			RGB=controladora.getCircuit().getLaps().get(i).getRgb();
			table.getModel().setValueAt(new Color(RGB[0],RGB[1],RGB[2]), i, 7);
			row = i+1;
    	}
		
		table.getModel().setValueAt(true, row, 0);
		table.getModel().setValueAt("AVERAGE", row, 1);
		String s = controladora.getCircuit().getStatistics().averageTime(controladora.getCircuit().getLaps()).toString();
		table.getModel().setValueAt(s, row, 2);
		table.getModel().setValueAt(controladora.getCircuit().getStatistics().averageTopSpeed(controladora.getCircuit().getLaps()), row, 3);
		table.getModel().setValueAt(controladora.getCircuit().getStatistics().averageAverageSpeed(controladora.getCircuit().getLaps()), row, 4);
		table.getModel().setValueAt(controladora.getCircuit().getStatistics().averageMinimumSpeed(controladora.getCircuit().getLaps()), row, 5);
		table.getModel().setValueAt(controladora.getCircuit().getStatistics().averageLength(controladora.getCircuit().getLaps()), row, 6);
		table.getModel().setValueAt(null, row, 7);
		
		row++;
		table.getModel().setValueAt(true, row, 0);
		table.getModel().setValueAt("TOTAL", row, 1);
		s = controladora.getCircuit().getStatistics().totalTime(controladora.getCircuit().getLaps()).toString();
		table.getModel().setValueAt(s, row, 2);
		table.getModel().setValueAt("---", row, 3);
		table.getModel().setValueAt("---", row, 4);
		table.getModel().setValueAt("---", row, 5);
		table.getModel().setValueAt(controladora.getCircuit().getStatistics().totalLength(controladora.getCircuit().getLaps()), row, 6);
		table.getModel().setValueAt(null, row, 7);
		} catch (Exception e) {
			
		}
	}

}